﻿using System.Net;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Optum.SN.Infra.Core.Cryptography.Test.IntegrationTests
{
    /// <summary>
    /// Tests Web API that exposes symmetric cryptography for single values.
    /// </summary>
    [TestClass]
    public class SymmetricCryptoWebTest : SymmetricCryptoWebTestBase
    {
        /// <summary>
        /// Tests single value encryption where the data to be encrypted is passed in via URI.
        /// </summary>
        [TestMethod]
        public void TestEncrypt()
        {
            var mock = new MockData().First();
            TestCrypto(mock.PlainText, "encrypt", actualPlainText => AssertCrypto(actualPlainText));
        }
        /// <summary>
        /// Tests single value decryption where the data to be decrypted is passed in via URI.
        /// </summary>
        [TestMethod]
        public void TestDecrypt()
        {
            var mock = new MockData().First();
            TestCrypto(mock.CipherText, "decrypt", actualPlainText => AssertDecrypt(mock.PlainText, actualPlainText));
        }
        /// <summary>
        /// Tests single value encryption using the older <see cref="WebClient"/> API.
        /// </summary>
        [TestMethod]
        public void TestEncryptNet35()
        {
            var mock = new MockData().First();
            TestCryptoNet35(mock.PlainText, "encrypt", actualPlainText => AssertCrypto(actualPlainText));
        }
        /// <summary>
        /// Tests single value decryption using the older <see cref="WebClient"/> API.
        /// </summary>
        [TestMethod]
        public void TestDecryptNet35()
        {
            var mock = new MockData().First();
            TestCryptoNet35(mock.CipherText, "decrypt", actualPlainText => AssertDecrypt(mock.PlainText, actualPlainText));
        }
        /// <summary>
        /// Tests single value encryption where the data to be encrypted is passed in via request body.
        /// </summary>
        [TestMethod]
        public void TestEncryptFromBody()
        {
            var mock = new MockData().First();
            TestCryptoFromBody(mock.PlainText, "encrypt", 
                content => AssertCrypto(content.ReadAsStringAsync().Result.Trim(@"""".ToCharArray())));
        }
        /// <summary>
        /// Tests single value decryption where the data to be decrypted is passed in via request body.
        /// </summary>
        [TestMethod]
        public void TestDecryptFromBody()
        {
            var mock = new MockData().First();
            TestCryptoFromBody(mock.CipherText, "decrypt",
                content => AssertDecrypt(mock.PlainText, content.ReadAsStringAsync().Result.Trim(@"""".ToCharArray())));
        }
    }
}